package com.yq.service.impl;

import com.yq.mapper.AdminMapper;
import com.yq.model.po.User;
import com.yq.model.vo.UserInfoVO;
import com.yq.service.AdminUserService;
import com.yq.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminUserServiceImpl implements AdminUserService {


    @Override
    public List<UserInfoVO> getAllUsersInfo() {

        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<UserInfoVO> list =  mapper.getAllUsersInfo();
        return list;
    }

    @Override
    public int deleteById(int userId) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count  = 0;
        try {
            count  = mapper.deleteById(userId);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }
        sqlSession.close();
        if (count==1){
            return 200;
        }
        return 404;
    }

    @Override
    public List<UserInfoVO> searchUser(String word) {

        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<UserInfoVO> list = mapper.searchUser(word);
        return list;
    }
}
